<template>
  <div class="login-container flex items-center justify-center min-h-screen bg-cover bg-center" :style="{ backgroundImage: `url(${require('@/assets/images/4.jpg')})` }">
    <div class="login-card bg-white p-8 rounded-lg shadow-md w-full max-w-md">
      <h2 class="text-2xl font-bold mb-6 text-center">登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" @submit.native.prevent="handleLogin">
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入账号" prefix-icon="el-icon-user" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" class="w-full custom-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.$http.post('/users/login', this.loginForm)
            .then(response => {
              if (response.data === '登录成功') {
                this.$message.success('登录成功');
                // 保存登录状态和用户名
                localStorage.setItem('token', 'some-token');
                localStorage.setItem('username', this.loginForm.account);
                // 使用 localStorage 替代 Vuex
                localStorage.setItem('isLoggedIn', 'true');
                localStorage.setItem('username', this.loginForm.account);
                // 跳转到首页
                this.$router.push('/');
              } else {
                this.$message.error(response.data);
              }
            })
            .catch(error => {
              this.$message.error('登录失败，请稍后重试');
              console.error('登录请求出错:', error);
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  height: 100vh; /* 修改为全屏高度 */
  width: 100vw; /* 修改为全屏宽度 */
  background-color: #f9fafb;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover; /* 确保背景图片覆盖整个容器 */
  background-position: center; /* 背景图片居中 */
}

.login-card {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.9); /* 半透明背景 */
  border-radius: 12px;
  max-width: 100%; /* 确保在小屏幕上也能全屏显示 */
  width: 100%; /* 确保在小屏幕上也能全屏显示 */
}

.el-form-item {
  margin-bottom: 20px;
}

.custom-input {
  border-radius: 8px;
  transition: border-color 0.3s ease;
}

.custom-input:focus {
  border-color: #2a5298;
}

.custom-button {
  background: #2a5298;
  border-color: #2a5298;
  color: #ffffff;
  transition: background 0.3s ease, transform 0.2s ease;
}

.custom-button:hover {
  background: #1e3c72;
  border-color: #1e3c72;
  transform: scale(1.05);
}
</style>